﻿using Domain.ValueObjects;
using Shared.Domain.Abstructs;
using Shared.Domain.Interface;

namespace Domain.Entities
{
    /// <summary>
    /// 用户属性管理
    /// </summary>
    public partial class User : BaseEntity<Guid>, IAggregateRoot
    {

        private readonly List<IDomainEvent> events = [];

        /// <summary>
        /// 领域事件集合
        /// </summary>
        public IReadOnlyList<IDomainEvent> Events => events;

        /// <summary>
        /// 用户名
        /// </summary>
        public NickName NickName { get; private set; }

        /// <summary>
        /// 用户手机号
        /// </summary>
        public PhoneNumber PhoneNumber { get; private set; }

        /// <summary>
        /// 用户密码哈希值
        /// </summary>
        public string PasswordHash { get; private set; }

        /// <summary>
        /// 学术信息
        /// </summary>
        public AcademicInfo? AcademicInfo { get; private set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool IsDeleted { get; private set;  }

        /// <summary>
        /// 用户信息构造
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="passwordHash"></param>
        private User(Guid id, NickName userName, PhoneNumber phoneNumber, string passwordHash, AcademicInfo? academicInfo) : base(id)
        {
            NickName = userName;
            PhoneNumber = phoneNumber;
            PasswordHash = passwordHash;
            AcademicInfo = academicInfo;
        }

        #pragma warning disable CS8618
        /// <summary>
        /// EF Core 专用的无参构造函数
        /// </summary>
        private User() { }

        /// <summary>
        /// 用户信息构造
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="phoneNumber"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static User Create(NickName userName, PhoneNumber phoneNumber, string password, AcademicInfo? academicInfo)
        {
            if (string.IsNullOrWhiteSpace(password))
                throw new ArgumentException("密码不能为空", nameof(password));

            return new User(Guid.NewGuid(), userName, phoneNumber, password,academicInfo);
        }
    }
}
